﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using eStream.Helpdesk.Notifier.Properties;
using System.Text.RegularExpressions;

namespace eStream.Helpdesk.Notifier
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : Window
    {
        private NotifierSettings settings = new NotifierSettings();
        public SettingsWindow()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(SettingsWindow_Loaded);
        }

        private void SettingsWindow_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = settings;

            //PasswordBox does not support databinding
            PasswordTextBox.Password = settings.Password;
        }

        private bool ValidateData(out string error)
        {
            error = String.Empty;

            if (!Regex.Match(settings.Email, @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}"
                                    + @"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\"
                                    + @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$").Success)
            {
                error = "Invalid E-mail address!";
                return false;
            }

            if (PasswordTextBox.Password.Length == 0)
            {
                error = "Password field cannot be empty!";
                return false;
            }

            var address = settings.HelpdeskAddress;

            if (String.IsNullOrWhiteSpace(address))
            {
                error = "Helpdesk address field cannot be empty!";
                return false;
            }

            if (!address.StartsWith("http://") && !address.StartsWith("https://"))
                address = "http://" + address;
            try
            {
                new Uri(address, UriKind.Absolute);

                settings.HelpdeskAddress = address;
            }
            catch (Exception)
            {
                error = "Invalid Helpdesk Address!";
                return false;
            }

            return true;
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            string error;
            if (!ValidateData(out error))
            {
                MessageBox.Show(error, "Invalid Data", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            settings.Password = PasswordTextBox.Password;
            settings.LastAttemptSuccessfull = false;
            settings.Save();

            this.DialogResult = true;
            this.Close();
        }

    }
}
